package coom.zwj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import coom.zwj.reggie.common.CustomException;
import coom.zwj.reggie.entity.Category;
import coom.zwj.reggie.entity.Dish;
import coom.zwj.reggie.entity.Setmeal;
import coom.zwj.reggie.mapper.CategoryMapper;
import coom.zwj.reggie.service.CategoryService;
import coom.zwj.reggie.service.DishService;
import coom.zwj.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类--判断分类中是否有所属菜品
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        //查询dish表中是否有菜品属于该分类
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        if (dishService.count(dishLambdaQueryWrapper) > 0) {
            throw new CustomException("该分类下有菜品,无法删除");
        }
        //查询setmeal表中是否有套餐属于该分类
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        if (setmealService.count(setmealLambdaQueryWrapper) > 0) {
            throw new CustomException("该分类下有套餐,无法删除");
        }
        super.removeById(id);
    }
}
